//
//  HZ_Find_CollectionViewCell.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HZ_Find_CollectionViewCell.h"
#import "HZ_Find_Two_CollectionReusableView.h"
#import "HZ_Find_Two_CollectionViewCell.h"
#import "HZ_Shuffling_CollectionReusableView.h"
#import "HZ_spring_Find_CollectionReusableView.h"
#import "HZ_hout_Find_CollectionReusableView.h"
#import "HZ_Listening_CollectionViewCell.h"
#import "HZ_hot_CollectionViewCell.h"
#import "HZ_Listenin_CollectionViewCell.h"
#import "HS_DataArray_Model.h"
#import "HZ_songs_Model.h"
#import "CSLPlayer.h"
#import "HSAFNetTool.h"
#import "MJRefresh.h"
#import "HZ_play_hs_Model.h"
@interface HZ_Find_CollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) NSMutableArray *urlArray;
@property (nonatomic, strong) UICollectionView *coll;
@property (nonatomic, strong) UICollectionViewFlowLayout *flow;
@property (nonatomic, assign) NSInteger playnumbea;
@property (nonatomic, strong) CSLPlayer *AVPlayer;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation HZ_Find_CollectionViewCell


-(instancetype)initWithFrame:(CGRect)frame{
   
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.flow = [[UICollectionViewFlowLayout alloc] init];
        
        _flow.itemSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height / 2);
        _flow.minimumInteritemSpacing = 0;
        _flow.minimumLineSpacing = 0;
        _flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.coll = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.contentView.bounds.size.height/20, self.contentView.bounds.size.width, self.contentView.bounds.size.height - self.contentView.bounds.size.height/10) collectionViewLayout:self.flow];
        _coll.backgroundColor = [UIColor whiteColor];
        
 //按钮cell重用池
        [self.coll registerClass:[HZ_Find_Two_CollectionViewCell class] forCellWithReuseIdentifier:@"HZ_Fin_Two_CollectionViewCell"];
// 大家在听cell
        [self.coll registerClass:[HZ_Listening_CollectionViewCell class] forCellWithReuseIdentifier:@"HZ_Listening_CollectionViewCell"];
// 热
        [self.coll registerClass:[HZ_hot_CollectionViewCell class] forCellWithReuseIdentifier:@"HZ_hot_CollectionViewCell"];
//   我是歌手
        [self.coll registerClass:[HZ_Listenin_CollectionViewCell class] forCellWithReuseIdentifier:@"HZ_Listenin_CollectionViewCell"];
//   轮播图分区头视图
        [self.coll registerClass:[HZ_Shuffling_CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HZ_Shuffling_CollectionReusableView"];
 //大家在听分区头视图cell 重用池
        [self.coll registerClass:[HZ_Find_Two_CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HZ_Find_Two_CollectionReusableView"];
//    火热分区头视图
        [self.coll registerClass:[HZ_hout_Find_CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HZ_hout_Find_CollectionReusableView"];
//        春天分区头视图
        [self.coll registerClass:[HZ_spring_Find_CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HZ_spring_Find_CollectionReusableView"];
        
        self.coll.delegate = self;
        self.coll.dataSource = self;
        [self.coll reloadData];
        [self gengduoData];
        
//        [self.coll addFooterWithTarget:self action:@selector(getInfo)];
//
//        [self.coll addHeaderWithTarget:self action:@selector(getUpdate)];
        
        [self.contentView addSubview:self.coll];
      
    }
    return  self;
}

-(void)setFindArray:(NSMutableArray *)findArray{
    _findArray = findArray;
    [self.coll reloadData];
}

//cell 个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 4;
    } else if (section == 1) {
        return 3;
    } else if (section == 2){
        return 6;
    } else if (section == 3){
        return 3;
    } else if (section == 4){
        return 3;
    }else if (section == 5){
        return 3;
    }else if (section == 6){
        return 3;
    }else if (section == 7) {
        return 4;
    } else {
    return 1;
    }
}
//cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        HZ_Find_Two_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_Fin_Two_CollectionViewCell" forIndexPath:indexPath];
        self.findModel = _findArray[indexPath.section + 1];
        HS_DataArray_Model *model = _findModel.data[indexPath.item];
        cell.model = model;
        return cell;
    } else if (indexPath.section == 1){
        HZ_Listening_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_Listening_CollectionViewCell" forIndexPath:indexPath];
        NSString *str = [NSString stringWithFormat:@"%ld", indexPath.item + 1];
        cell.sortlabel.text = str;
        self.findModel = _findArray[indexPath.section + 1];
        HS_DataArray_Model *model = self.findModel.data[0];
        HZ_songs_Model *sonmodel = model.songs[indexPath.item];
        cell.model = sonmodel;
        return cell;
    } else if (indexPath.section == 2){
        HZ_hot_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_hot_CollectionViewCell" forIndexPath:indexPath];
        self.findModel = _findArray[indexPath.section + 1];
        HS_DataArray_Model *model = _findModel.data[indexPath.item];
        cell.model = model;
        return cell;
    }else if (indexPath.section == 3){
        HZ_hot_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_hot_CollectionViewCell" forIndexPath:indexPath];
        self.findModel = _findArray[indexPath.section + 1];
        HS_DataArray_Model *model = _findModel.data[indexPath.item];
        cell.model = model;
        return cell;
    } else if (indexPath.section == 4){
        HZ_Listenin_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_Listenin_CollectionViewCell" forIndexPath:indexPath];
        self.findModel = _findArray[indexPath.section + 1];
        HS_DataArray_Model *model = _findModel.data[indexPath.item];
        cell.model = model;
        return cell;
    }else if (indexPath.section == 5){
        HZ_hot_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_hot_CollectionViewCell" forIndexPath:indexPath];
        self.findModel = _findArray[indexPath.section + 1];
        HS_DataArray_Model *model = _findModel.data[indexPath.item];
        cell.model = model;
        return cell;
    }else if (indexPath.section == 6){
        HZ_hot_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_hot_CollectionViewCell" forIndexPath:indexPath];
        self.findModel = _findArray[indexPath.section + 1];
        HS_DataArray_Model *model = _findModel.data[indexPath.item];
        cell.model = model;
        return cell;
    }else if (indexPath.section == 7){
        HZ_Listenin_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_Listenin_CollectionViewCell" forIndexPath:indexPath];
        self.findModel = _findArray[indexPath.section + 1];
        HS_DataArray_Model *model = _findModel.data[indexPath.item];
        cell.model = model;
        return cell;
        
    }else{
        
        HZ_Listenin_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_Listenin_CollectionViewCell" forIndexPath:indexPath];
        self.findModel = _findArray[indexPath.section + 1];
        
        HS_DataArray_Model *model = _findModel.data[indexPath.item];
        
        cell.model = model;
        return cell;
 }
}
//分区头视图cell 创建
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
        if (kind == UICollectionElementKindSectionHeader && indexPath.section == 0) {
            HZ_Shuffling_CollectionReusableView *shuffling = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HZ_Shuffling_CollectionReusableView" forIndexPath:indexPath];
            self.imageArray = [NSMutableArray arrayWithCapacity:0];
            self.urlArray = [NSMutableArray arrayWithCapacity:0];
            self.findModel = _findArray[0];
            for (HS_DataArray_Model *model in _findModel.data) {
                [self.imageArray addObject:model.picUrl];
                [_urlArray addObject:model.diction.value];
                
            }
            shuffling.urlarray = _urlArray;
            shuffling.array = self.imageArray;
            return shuffling;
        } else if (kind == UICollectionElementKindSectionHeader && indexPath.section == 1){
            HZ_Find_Two_CollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HZ_Find_Two_CollectionReusableView" forIndexPath:indexPath];
           header.model = _findArray[indexPath.section + 1];
            return header;
        } else if (kind == UICollectionElementKindSectionHeader && indexPath.section == 2){
            HZ_hout_Find_CollectionReusableView *hout = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HZ_hout_Find_CollectionReusableView" forIndexPath:indexPath];
            hout.age = indexPath.section;
            hout.model = _findArray[ indexPath.section + 1];
            return hout;
        }else if (kind == UICollectionElementKindSectionHeader && indexPath.section == 3) {
            HZ_spring_Find_CollectionReusableView *spring = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HZ_spring_Find_CollectionReusableView" forIndexPath:indexPath];
            spring.model = _findArray[indexPath.section + 1];
            return spring;
        } else if (kind == UICollectionElementKindSectionHeader && indexPath.section == 4){
            HZ_hout_Find_CollectionReusableView *hout = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HZ_hout_Find_CollectionReusableView" forIndexPath:indexPath];
            hout.age = indexPath.section;
            hout.model = _findArray[indexPath.section + 1];
            return hout;
        }else if (kind == UICollectionElementKindSectionHeader && indexPath.section == 5){
            HZ_spring_Find_CollectionReusableView *spring = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HZ_spring_Find_CollectionReusableView" forIndexPath:indexPath];
            spring.model = _findArray[indexPath.section + 1];
            return spring;
        }else if (kind == UICollectionElementKindSectionHeader && indexPath.section == 6){
            HZ_hout_Find_CollectionReusableView *hout = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HZ_hout_Find_CollectionReusableView" forIndexPath:indexPath];
            hout.age = indexPath.section;
            hout.model = _findArray[indexPath.section + 1];
            return hout;
        }else if (kind == UICollectionElementKindSectionHeader && indexPath.section == 7){
            HZ_spring_Find_CollectionReusableView *spring = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HZ_spring_Find_CollectionReusableView" forIndexPath:indexPath];
            spring.model = _findArray[indexPath.section + 1];
            return spring;
        }else {
            HZ_spring_Find_CollectionReusableView *spring = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HZ_spring_Find_CollectionReusableView" forIndexPath:indexPath];
            spring.model = _findArray[indexPath.section + 1];
            return spring;
        }
}
//分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 9;
}
//item 高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(self.bounds.size.width / 4, self.bounds.size.height / 7);
    } else if (indexPath.section == 1){
        return CGSizeMake(self.bounds.size.width, self.bounds.size.height / 5);
    } else if (indexPath.section == 2){
       return CGSizeMake(self.bounds.size.width / 3, self.bounds.size.height / 4);
    } else if (indexPath.section == 3) {
    return CGSizeMake(self.bounds.size.width / 3, self.bounds.size.height / 4);
    } else if (indexPath.section == 4) {
       return CGSizeMake(self.bounds.size.width , self.bounds.size.height / 4.5);
    } else if (indexPath.section == 5){
          return CGSizeMake(self.bounds.size.width / 3, self.bounds.size.height / 4);
    }else if (indexPath.section == 6){
          return CGSizeMake(self.bounds.size.width / 3, self.bounds.size.height / 4);
    }else if (indexPath.section == 7){
        return CGSizeMake(self.bounds.size.width , self.bounds.size.height / 4.5);
    }
    return CGSizeMake(self.bounds.size.width , self.bounds.size.height / 4);
}
//分区头视图高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(self.bounds.size.width, 200);
    }
    return CGSizeMake(self.bounds.size.width, 50);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.item == 0) {
            
            NSNotificationCenter *cellfenlei = [NSNotificationCenter defaultCenter];
            
            [cellfenlei postNotificationName:@"1" object:nil userInfo:nil];
            
        } else if (indexPath.item == 1){
            
            NSNotificationCenter *cellfenleiTwo = [NSNotificationCenter defaultCenter];

            [cellfenleiTwo postNotificationName:@"2" object:nil userInfo:nil];
     
        } else if (indexPath.item == 3) {
            
            [self.delegate jumpSinger];
            
        } else if (indexPath.item == 2){

            NSNotificationCenter *cellfenleiThree = [NSNotificationCenter defaultCenter];
            
            
            [cellfenleiThree postNotificationName:@"3" object:nil userInfo:nil];
            
            
        }
    } else if (indexPath.section == 1) {
        
//        _AVPlayer = [CSLPlayer shareAVPlayer];
        
        [CSLPlayer shareAVPlayer].isbutton = YES;
        _playnumbea = indexPath.row;

        
        
        NSNotificationCenter *playnumbea = [NSNotificationCenter defaultCenter];
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        [array addObject:_dataSource];
        NSString *str = [NSString stringWithFormat:@"%ld",_playnumbea];
        
        [array addObject:str];
        
        [playnumbea postNotificationName:@"7" object:array userInfo:nil];
        
       
        
    } else if (indexPath.section == 2 || indexPath.section == 3 || indexPath.section == 4 || indexPath.section == 5 || indexPath.section == 6 || indexPath.section == 7 || indexPath.section == 8) {
        
        [self.delegate jumpPlayList:indexPath];
        
    }
    
   
    
    
}

-(void)gengduoData{
    
    [HSAFNetTool GETNetWithUrl:@"http://api.favorite.ttpod.com/favorite/song/plaza?f=384&os=4.4.2&alf=10002266&imei=864783024887313&random=0&from=android&resolution=720x1280&net=2&api_version=1.0&agent=none&v=v8.4.1.2016032512&utdid=VRu%2Blc%2FVSokDANuKLLSuSY%2B5&address=%E8%BE%BD%E5%AE%81%E7%9C%81%E5%A4%A7%E8%BF%9E%E5%B8%82%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%BA%94%E4%B8%80%E8%B7%AF314%E5%8F%B7&longitude=121.544426&user_id=0&latitude=38.883442&language=zh" body:nil headerFile:nil response:HSJSON success:^(id result) {
        
        
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"data"][@"songs"]) {
            
            
            HZ_play_hs_Model *songListModel = [[HZ_play_hs_Model alloc] initWithDataSource:dic];
            [self.dataSource addObject:songListModel];
            
            
        }
        
        [_coll reloadData];
    } failure:^(NSError *error) {
        
    }];
    
    
    
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
